package com.goldcn.classify.service;

import com.goldcn.classify.dao.ClassifyMapper;
import com.goldcn.classify.dao.ClassifyStyleMapper;
import com.goldcn.classify.dao.StyleMapper;
import com.goldcn.classify.dto.ClassifyStyleDto;
import com.goldcn.classify.model.Classify;
import com.goldcn.classify.model.ClassifyStyle;
import com.goldcn.classify.model.Style;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/9.
 */
@Service("classifyStyleService")
@Transactional
public class ClassifyStyleService {
    @Autowired
    private ClassifyStyleMapper classifyStyleMapper;

    @Autowired
    private ClassifyMapper classifyMapper;

    @Autowired
    private StyleMapper styleMapper;

    public int deleteByPrimaryKey(String csid){
        return classifyStyleMapper.deleteByPrimaryKey(csid);
    }

    public int insert(ClassifyStyle record){
        return classifyStyleMapper.insert(record);
    }

    public int insertSelective(ClassifyStyle record){
        if(classifyMapper.selectByPrimaryKey(record.getClazzid()).equals(null)){
            throw new ServiceException("类目id无检索信息", RestConst.ErrorCode.DATABASE_ERROR);
        }else if(classifyMapper.selectByPrimaryKey(record.getStyleid()).equals(null)){
            throw new ServiceException("款式id无检索信息", RestConst.ErrorCode.DATABASE_ERROR);
        }else {
            record.setCsid(UuidUtils.uuid());
            return classifyStyleMapper.insertSelective(record);
        }
    }

    @Transactional
    public boolean insertlist(List<Map<String,String>> list){
        int times=0;
        for (Map<String,String> map:list) {
            ClassifyStyle record=new ClassifyStyle();
            record.setClazzid(map.get("clazzid"));
            record.setStyleid(map.get("styleid"));
            if(classifyMapper.selectByPrimaryKey(record.getClazzid()).equals(null)){
                throw new ServiceException("类目id无检索信息", RestConst.ErrorCode.DATABASE_ERROR);
            }else if(styleMapper.selectByPrimaryKey(record.getStyleid()).equals(null)){
                throw new ServiceException("款式id无检索信息", RestConst.ErrorCode.DATABASE_ERROR);
            }else {
                times++;
                record.setCsid(UuidUtils.uuid());
                classifyStyleMapper.insertSelective(record);
            }
        }
        if(times==list.size()){
            return true;
        }else{
            return false;
        }
    }

    public ClassifyStyle selectByPrimaryKey(String csid){
        ClassifyStyle classifyStyle=new ClassifyStyle();
        classifyStyle=classifyStyleMapper.selectByPrimaryKey(csid);
        if(classifyStyle==null){
            throw new ServiceException("关联id不存在", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return classifyStyle;
    }

    public int updateByPrimaryKeySelective(ClassifyStyle record){
        return classifyStyleMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(ClassifyStyle record){
        return classifyStyleMapper.updateByPrimaryKey(record);
    }

    public List<Map<String,Object>> loadByClassify(String clazzid){
        Map<String,String> parm=new HashMap<String,String>();
        parm.put("clazzid",clazzid);
        return classifyStyleMapper.loadObjInfo(parm);
    }

    public List<Map<String,Object>> loadCsIDByClassify(String clazzid){
        Map<String,String> parm=new HashMap<String,String>();
        parm.put("clazzid",clazzid);
        return classifyStyleMapper.loadObjInfo(parm);
    }

    public List<Map<String,Object>> loadClassify(String styleid){
        Map<String,String> parm=new HashMap<String,String>();
        parm.put("styleid",styleid);
        return classifyStyleMapper.loadObjInfo(parm);
    }


    @Transactional
    public boolean addOrUpdatelist(ClassifyStyleDto list){
        //先删除所有
        List<Map<String,Object>> list1=this.loadCsIDByClassify(list.getClazzid());
        for(int i=0;i<list1.size();i++){
            classifyStyleMapper.deleteByPrimaryKey(
                    list1.get(i).get("gd_csID").toString());
        }
        int times=0;
        for(int i=0;i<list.getStylelist().size();i++){
            ClassifyStyle classifyStyle=new ClassifyStyle();
            classifyStyle.setCsid(UuidUtils.uuid());
            classifyStyle.setClazzid(list.getClazzid());
            classifyStyle.setStyleid(list.getStylelist().get(i).toString());
            times+=classifyStyleMapper.insertSelective(classifyStyle);
        }
        if(times==list.getStylelist().size()){
            return true;
        }else{
            return false;
        }
    }
}
